package com.wyz.saas.module.chat.bean

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.wyz.saas.common.room.AbstractUpdateEntity
import java.util.*

@Entity(tableName = "table_user")
class User : AbstractUpdateEntity(){

    @ColumnInfo(name = "name")
    var name: String? = null

    @ColumnInfo(name = "phone")
    var phone: String? = null

    @ColumnInfo(name = "portrait")
    var portrait: String? = null

    @ColumnInfo(name = "desc")
    var desc: String? = null

    @ColumnInfo(name = "sex")
    var sex = 0

    // 我对某人的备注信息，也应该写入到数据库中
    @ColumnInfo(name = "alias")
    var alias: String? = null

    // 用户关注人的数量
    @ColumnInfo(name = "follows")
    var follows = 0

    // 用户粉丝的数量
    @ColumnInfo(name = "following")
    var following = 0

    // 我与当前User的关系状态，是否已经关注了这个人
    @ColumnInfo(name = "isFollow")
    var isFollow = false

    // 时间字段
    @ColumnInfo(name = "modifyAt")
    var modifyAt: String? = null

    override fun toString(): String {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", portrait='" + portrait + '\'' +
                ", desc='" + desc + '\'' +
                ", sex=" + sex +
                ", alias='" + alias + '\'' +
                ", follows=" + follows +
                ", following=" + following +
                ", isFollow=" + isFollow +
                ", modifyAt=" + modifyAt +
                '}'
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false
        val user = other as User
        return (sex == user.sex
                && follows == user.follows
                && following == user.following
                && isFollow == user.isFollow
                && id == user.id
                && name == user.name
                && phone == user.phone
                && portrait == user.portrait
                && desc == user.desc
                && alias == user.alias
                && modifyAt == user.modifyAt)
    }

    override fun hashCode(): Int {
        return if (id != null) id.hashCode() else 0
    }

    fun isSame(old: User): Boolean {
        // 主要关注Id即可
        return this == old || Objects.equals(id, old.id)
    }

    fun isUiContentSame(old: User): Boolean {
        // 显示的内容是否一样，主要判断 名字，头像，性别，是否已经关注
        return this == old || (Objects.equals(name, old.name)
                && Objects.equals(portrait, old.portrait)
                && Objects.equals(sex, old.sex)
                && Objects.equals(isFollow, old.isFollow))
    }
}